<?php
class mis_log extends Model 
{
    public static function getTableName()
    {
        return 'mis_log';
    }

    public function getPrimaryKey()
    {
		return array('pdes_id','type');
	}

    public function update($data)/*{{{*/
    {
		if(empty($data->pdes_id) || empty($data->type)) return false;

		$now_time = date("Y-m-d H:i:s");
		$user     = CommonUtils::get_user();

		$default  = new mis_log();
		$default->pdes_id     = $data->pdes_id;
		$default->type        = $data->type;
		$default->sync_data   = array();
		$default->mis_data    = array();
		$default->status      = 1;
		$default->message     = '';
		$default->recover_sql = array();
		$default->created_at  = $now_time;
		$default->created_by  = $user;
		$default->modified_at = $now_time;
		$default->modified_by = $user;

		$exists = Model::$dbo['r']->findInstance('mis_log',array($data->pdes_id,$data->type));
		if($exists){
			$obj = Model::diff($exists,$data);
		}else{
			$obj = Model::diff($default,$data);
		}


		if((is_object($obj->mis_data)    || is_array($obj->mis_data))){    $obj->mis_data    = json_encode($obj->mis_data); }
		if((is_object($obj->sync_data)   || is_array($obj->sync_data))){   $obj->sync_data   = json_encode($obj->sync_data); }
		if((is_object($obj->recover_sql) || is_array($obj->recover_sql))){ $obj->recover_sql = json_encode($obj->recover_sql); }

		return Model::$dbo['w']->save($obj);
	}/*}}}*/

    public function getResultList($findRelation, $field, $oper, $value, $start, $length, $sidx, $sord)/*{{{*/
    {
        $status = array(
            '0' => 'failure',
            '1' => 'success'
        );

        $where = array();
        if (!empty($field)) {
            foreach ($field as $field_key=>$field_value) {
                switch ($field_value) {
                    case 'pdes_type':
                        $where[] = "`type` = '" . $value[$field_key] . "'";
                        break;
					case 'pdes_id':
                        $where[] = "`pdes_id` = '" . $value[$field_key] . "'";
						break;
                    case 'modified_at_begin':
                        $where[] = "`modified_at` >= '" . (string)$value[$field_key] . "'";
                        break;
                    case 'modified_at_end':
                        $where[] = "`modified_at` <= '" . $value[$field_key] . " 23:59:59'";
                        break;
                }
            }
        }

        $sql = "SELECT `pdes_id`, `type`, `status`, `message`, `modified_at`, `modified_by` FROM `mis_log`";

        #where
        if (!empty($where)) {
            $sql .= ' WHERE ' . implode(' AND ', $where);
        }

        #total
        $total_sql = "SELECT COUNT(*) AS num FROM ({$sql}) AS tmp";
        $total = Model::selectBySql($total_sql);

        $ret['page'] = ceil($start/$length) + 1;;
        $ret['total'] = ceil($total[0]['num']/$length);
        $ret['records'] = $total[0]['num'];

        #order by
        if ($sidx && $sord) {
            if ($sidx == 'id') {
                $sidx = 'pdes_id';
            }
            $sql .= ' ORDER BY ' . $sidx . ' ' . strtoupper($sord);
        }

        #limit
        $sql .= " LIMIT {$start}, {$length}";

        $result = Model::selectBySql($sql);
        if (!empty($result)) {
            foreach ($result as $row) {
                $record = array();
                $record['id'] = $row['pdes_id']; 
                $success = $row['status'];
                $row['status'] = $status[$row['status']];
                $record['cell'] = array_values($row);
                $record['cell'][] = $success ? '<a href="pms_detail.html?act=view&pdes_type='.$row['type'].'&id='.$row['pdes_id'].'&type=mis" title="View" class="btn ui-state-default ui-corner-all"><span class="ui-icon ui-icon-image"></span>View</a>' : ''; 
                $ret['rows'][] = $record;
            }
        }

        return $ret;
    }/*}}}*/
}
?>
